/**
 * 所有版权归 广西梧州 陈锦韬 所有
 *
 * @Title: CommonService
 * @Package com.rx.stang.service
 * @Description: 公共接口
 * @author: 陈锦韬
 * @date: 2022/4/17
 * @version V1.0
 * @Copyright: 2022 陈锦韬  All rights reserved.
 */
package com.rx.stang.service;

import com.rx.core.base.RxBaseData;
import com.rx.core.enm.HANDLER_TYPE;
import com.rx.core.enm.RESULT_CODE;
import com.rx.core.service.RxBaseAbstractService;
import com.rx.core.service.StandardService;
import com.rx.core.util.EmptyChecker;
import com.rx.core.util.RxUtil;
import com.rx.stang.handler.SuiTangHandler;
import com.rx.stang.vo.ColumnistInfoVo;
import com.rx.stang.vo.TangColumnistInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestBody;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author: CHENJINTAO170
 * @Description: 公共接口
 * @date: 2022/4/17
 */
@Slf4j
@Service
public class STangOperateService extends RxBaseAbstractService {
    public static final String  NEWEST="1";
    public static final String  OLDE="0";
    public static final int MIN_RETURN_SIZE = 15;
    @Autowired
    StandardService standardService;

    @Autowired
    SuiTangHandler suiTangHandler;

    @Transactional
    public Object shelves(@RequestBody List<ColumnistInfoVo> dataList)  {

        if(EmptyChecker.isEmpty(dataList)){
            RxUtil.CheckUtil.throwOut(RESULT_CODE.FAIL,"参数为空！");
        }
        List<String> titleList = dataList.stream().map(ColumnistInfoVo::getTitle).collect(Collectors.toList());
        if(!suiTangHandler.shelvesColumnist(titleList,NEWEST,OLDE)){
            RxUtil.CheckUtil.throwOut(RESULT_CODE.FAIL,"旧寄语下架失败！");
        }


        List<RxBaseData> resultList = new ArrayList<>();
        for (ColumnistInfoVo info : dataList) {
            TangColumnistInfo tangColumnistInfo = new TangColumnistInfo();
            tangColumnistInfo.setIsNewest(NEWEST);
            tangColumnistInfo.setBid(info.getBid());
            tangColumnistInfo.setVer(info.getVer());
            resultList.add(tangColumnistInfo);
        }

        try {

            TangColumnistInfo condition = (TangColumnistInfo) RxBaseData.create(TangColumnistInfo.class, HANDLER_TYPE.SINGLE_UPDATE);
            condition.setDataList(resultList);
            standardService.batchUpdate(condition);

        } catch (IllegalAccessException | InstantiationException e) {
            RxUtil.CheckUtil.throwOut(RESULT_CODE.FAIL,"寄语上架失败！");
        }

        return Integer.valueOf(1);
    }
}
